package com.kbt.listener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.kbt.activity.AddressMangeActivity;
import com.kbt.activity.GoodForPayActivity;
import com.kbt.activity.R;
import com.kbt.adapter.OrderReturnAdapter;
import com.kbt.commont.Constants;
import com.kbt.model.data.BaseBean;
import com.kbt.model.data.OrderBean;
import com.kbt.net.RequestManager;
import com.kbt.util.utils.Utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhgk
 * @package: com.kbt.listener
 * @TODO: 保存快递信息
 * @data: 2015/8/15 16:31
 * @version: V1.0
 */
public class SaveKuaiDiInfoListener implements View.OnClickListener,RequestManager.ResponseInterface{
    private Activity activity;
    private TextView kuaiDiName;
    private TextView kuaiDiHao;
    private OrderBean orderBean;
    private RequestManager mRequestManager;
    private Map<String, String> map =null;
    private BaseBean baseBean;
    private Button save;
    private Spinner name;
    private Map<String,String> kuaiDiMap = null;

    public SaveKuaiDiInfoListener(Activity activity, TextView kuaiDiName, TextView kuaiDiHao,Button save,OrderBean orderBean) {
        this.activity = activity;
        this.kuaiDiName = kuaiDiName;
        this.kuaiDiHao = kuaiDiHao;
        this.orderBean = orderBean;
        this.save = save;
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, String>();

    }
    public SaveKuaiDiInfoListener(Activity activity, Spinner kuaiDiName, TextView kuaiDiHao,Button save,OrderBean orderBean) {
        this.activity = activity;
        this.name = kuaiDiName;
        this.kuaiDiHao = kuaiDiHao;
        this.orderBean = orderBean;
        this.save = save;
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, String>();
        kuaiDiMap = new HashMap<String,String>();
        kuaiDiMap.put("顺丰快递","sf");
        kuaiDiMap.put("申通快递","sto");
        kuaiDiMap.put("圆通快递","yt");
        kuaiDiMap.put("韵达快递","yd");
        kuaiDiMap.put("天天快递","tt");
        kuaiDiMap.put("EMS","ems");
        kuaiDiMap.put("中通快递","zto");
        kuaiDiMap.put("汇通快递","ht");
    }

    /*********
     * 点击按钮打开Activity
     *
     * @param v
     */
    @Override
    public void onClick(View v) {

        if(!"".equals(name)&&!"".equals(kuaiDiHao.getText().toString())){
           //保存退货物流信息
            httpSaveWuliuInfo();
        }else{
            Toast.makeText(activity, "请填写快递公司和快递单号!", Toast.LENGTH_SHORT).show();
        }
    }
    //保存退货物流信息
    private void httpSaveWuliuInfo(){
        if(Utils.isNetworkAvailable(activity)) {
            map.clear();
            map.put("methodType", "updateThKd");
            Gson gson = new Gson();
            Map dataMap = new HashMap<String,String>();
            dataMap.put("order_no", orderBean.getOrder_no());
            dataMap.put("th_kd_mc", name.getSelectedItem().toString());
            dataMap.put("th_kd_dh", kuaiDiHao.getText().toString());
            dataMap.put("th_kd_no", kuaiDiMap.get(name.getSelectedItem().toString()));
            map.put("data", gson.toJson(dataMap));
            mRequestManager.post(Constants.ServiceInterFace.saveWuliuURL, map, Constants.RequestTag.saveWuLiuTag, BaseBean.class);
        }else{
            Toast.makeText(activity, R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        if (tag == Constants.RequestTag.saveWuLiuTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                baseBean = (BaseBean) parsedGSON;
                if(baseBean.getResult().equals("success")){
                    save.setVisibility(View.INVISIBLE);
////                    kuaiDiName.setEnabled(false);
                    name.setEnabled(false);
                    kuaiDiHao.setEnabled(false);
                }else{
                    Toast.makeText(activity,""+baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        Toast.makeText(activity,"error",Toast.LENGTH_SHORT).show();
    }
}
